package cwinsor.com.leaf_raking;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import cwinsor.com.leaf_raking.Person.Format;

public class FileTypeRoster extends FileType {

	/* 
	 * local variables - column numbers
	 */
	protected Integer colSkip;

	protected Integer colRowerCox;
	protected Integer colRowerVarNov;
	protected Integer colRowerFirstName;
	protected Integer colRowerLastName;
	protected Integer colRowerGender;
	protected Integer colRowerSchool;
	protected Integer colRowerGrade;
	protected Integer colRowerEmail;
	protected Integer colRowerPhoneC;

	protected Integer colPhoneH;
	protected Integer colStreet;

	protected Integer colParent1FirstName;
	protected Integer colParent1LastName;
	protected Integer colParent1Email;
	protected Integer colParent1PhoneC;

	protected Integer colParent2FirstName;
	protected Integer colParent2LastName;
	protected Integer colParent2Email;
	protected Integer colParent2PhoneC;

	protected Integer colEnd;



	/**
	 * constructor
	 */
	public FileTypeRoster() {

		// column numbers
		colSkip = 0;

		colRowerCox = 1;
		colRowerVarNov = 2;
		colRowerFirstName = 3;
		colRowerLastName = 4;
		colRowerGender = 5;
		colRowerSchool = 6;
		colRowerGrade = 7;
		colRowerEmail = 8;
		colRowerPhoneC = 9;

		colPhoneH = 10;
		colStreet = 11;

		colParent1FirstName = 14;
		colParent1LastName = 15;
		colParent1Email = 16;
		colParent1PhoneC = 17;

		colParent2FirstName = 18;
		colParent2LastName = 19;
		colParent2Email = 20;
		colParent2PhoneC = 21;

		colEnd = 25;
	}


	// name
	public String getName() { return "FileTypeRoster"; }


	///////////////
	// column getters
	protected Integer colSkip() { return colSkip; }
	protected Integer colEnd() { return colEnd; }

	protected Integer colRowerCox() { return colRowerCox; }
	protected Integer colRowerVarNov() { return colRowerVarNov; }
	protected Integer colRowerFirstName() { return colRowerFirstName; }
	protected Integer colRowerLastName() { return colRowerLastName; }
	protected Integer colRowerGender() { return colRowerGender; }
	protected Integer colRowerSchool() { return colRowerSchool; }
	protected Integer colRowerGrade() { return colRowerGrade; }
	protected Integer colRowerEmail() { return colRowerEmail; }
	protected Integer colRowerPhoneC() { return colRowerPhoneC; }

	protected Integer colPhoneH() { return colPhoneH; }
	protected Integer colStreet() { return colStreet; }

	protected Integer colParent1FirstName() { return colParent1FirstName; }
	protected Integer colParent1LastName() { return colParent1LastName; }
	protected Integer colParent1Email() { return colParent1Email; }
	protected Integer colParent1PhoneC() { return colParent1PhoneC; }

	protected Integer colParent2FirstName() { return colParent2FirstName; }
	protected Integer colParent2LastName() { return colParent2LastName; }
	protected Integer colParent2Email() { return colParent2Email; }
	protected Integer colParent2PhoneC() { return colParent2PhoneC; }



	@Override
	public void doPopulate(
			ArrayList<String> theFieldsObject,
			ArrayList<Person> persons,
			Boolean supervisors) {

		// if the line starts with "SKIP" then skip it
		if (theFieldsObject.get(colSkip()).equals("SKIP")) {
			Msg.note(dS + ": skipping line" + theFieldsObject.toString());
			return;
		}

		Msg.note(dS + ": populating from line" + theFieldsObject.toString());

		// sanity checks
		// confirm there are enough fields
		if (theFieldsObject.size() != colEnd()+1) {
			Msg.fatal(dS + ": number of columns incorrect..."
					+ " last column (index) = " + colEnd()
					+ " but theFieldsObject.size() = "
					+ theFieldsObject.size()
					+ " (should be +1) -- IS THERE A \",\" IN THE DATA ITSELF ?!!!");
		}
		// confirm the line ends with "END"
		if (!theFieldsObject.get(colEnd()).equals("END")) {
			Msg.fatal(dS + ": the last column (number " + colEnd() + ") should be \"END\"");
		}


		if (!supervisors) {
			// raker
			Person raker = new PersonRaker();
			raker.setFirstName(theFieldsObject.get(colRowerFirstName()));
			raker.setLastName(theFieldsObject.get(colRowerLastName()));
			raker.setGender(theFieldsObject.get(colRowerGender()));

			if (theFieldsObject.get(colRowerCox).equals("coxswain")) {
				raker.setPower(1);
			}
			else if (theFieldsObject.get(colRowerVarNov).equals("v") && theFieldsObject.get(colRowerGender).equals("Male")) {
				raker.setPower(5);
			}
			else if (theFieldsObject.get(colRowerVarNov).equals("v") && theFieldsObject.get(colRowerGender).equals("Female")) {
				raker.setPower(4);
			}
			else if (theFieldsObject.get(colRowerVarNov).equals("n") && theFieldsObject.get(colRowerGender).equals("Male")) {
				raker.setPower(3);
			}
			else if (theFieldsObject.get(colRowerVarNov).equals("n") && theFieldsObject.get(colRowerGender).equals("Female")) {
				raker.setPower(2);
			}
			else {
				Msg.fatal(dS + ": hit the default case - "
						+ " colRowerCox = " + theFieldsObject.get(colRowerCox) 
						+ " colRowerVarNov " + theFieldsObject.get(colRowerVarNov)
						+ " colRowerGender " + theFieldsObject.get(colRowerGender));

			}

			raker.setPhoneC(theFieldsObject.get(colRowerPhoneC()));
			raker.setPhoneH(theFieldsObject.get(colPhoneH()));
			raker.setStreet(theFieldsObject.get(colStreet()));
			persons.add(raker);
		}

		if (supervisors) {
			// parent 1
			Person parent1 = new PersonSupervisor();
			parent1.setFirstName(theFieldsObject.get(colParent1FirstName()));
			parent1.setLastName(theFieldsObject.get(colParent1LastName()));
			parent1.setGender("x");
			parent1.setPhoneC(theFieldsObject.get(colParent1PhoneC()));
			parent1.setPhoneH(theFieldsObject.get(colPhoneH()));
			parent1.setStreet(theFieldsObject.get(colStreet()));
			persons.add(parent1);

			// parent 2
			Person parent2 = new PersonSupervisor();
			parent2.setFirstName(theFieldsObject.get(colParent2FirstName()));
			parent2.setLastName(theFieldsObject.get(colParent2LastName()));
			parent2.setGender("x");
			parent2.setPhoneC(theFieldsObject.get(colParent2PhoneC()));
			parent2.setPhoneH(theFieldsObject.get(colPhoneH()));
			parent2.setStreet(theFieldsObject.get(colStreet()));
			persons.add(parent2);
		}
	}


	@Override
	protected void doWriteToFile(Person person, PrintWriter writer,
			Format format) {
		Msg.fatal(dS + ": not supported for FileTypeRoster");
	}


	@Override
	protected void doApplySchedulePreferenceUpdates(ArrayList<String> theFieldsObject,
			PrsnList personList) {

		Msg.fatal(dS + ": not supported");
	}


	// debug
	String dS = this.getClass().getSimpleName();
}
